<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="queryParams.carNumber" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="queryParams.personName" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select v-model="queryParams.cardStatus">
        <el-option label="可用" :value="0" />
        <el-option label="已过期" :value="1" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="queryBtn">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/addCard')">添加月卡</el-button>
      <el-button @click="delBtns">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="list" @selection-change="handleSelectionChange">
        <el-table-column type="selection" />
        <el-table-column type="index" label="序号" />
        <el-table-column prop="personName" label="车主名称" />
        <el-table-column prop="phoneNumber" label="联系方式" />
        <el-table-column prop="carNumber" label="车牌号码" />
        <el-table-column prop="carBrand" label="车辆品牌" />
        <el-table-column prop="totalEffectiveDate" label="剩余有效天数" />
        <el-table-column prop="cardStatus" label="状态">
          <template #default="{row}">
            <span>{{ row.cardStatus === 0 ? '可用' : '已过期' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{row}">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text">查看</el-button>
            <el-button size="mini" type="text" @click="$router.push(`/addCard/${row.id}`)">编辑</el-button>
            <el-popconfirm title="是否确定删除月卡？" @confirm="delBtn(row.id)">
              <el-button slot="reference" size="mini" type="text" style="margin-left: 10px;">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next, sizes"
        :page-size.sync="queryParams.pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        background
        :current-page.sync="queryParams.page"
        @current-change="getCardList"
        @size-change="handleSizeChange"
      />
    </div>
    <!-- 添加楼宇 -->
    <el-dialog title="添加楼宇" width="580px">
      <!-- 表单接口 -->
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form> -->
      </div>
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { delCardAPI, getCardListAPI } from '@/api/car'

export default {
  data() {
    return {
      list: [],
      queryParams: {
        page: 1,
        pageSize: 10,
        carNumber: '',
        personName: '',
        cardStatus: null
      },
      total: 0,
      selectedCarList: []
    }
  },
  created() {
    this.getCardList()
  },
  methods: {
    // 获取月卡信息列表
    async getCardList() {
      const { data: { rows, total }} = await getCardListAPI(this.queryParams)
      // console.log(data)
      this.list = rows
      this.total = total
    },
    // 显示条数变化
    handleSizeChange() {
      this.queryParams.page = 1
      this.getCardList()
    },
    // 搜索业务
    async queryBtn() {
      this.queryParams.page = 1
      await this.getCardList()
      this.queryParams.carNumber = ''
      this.queryParams.personName = ''
      this.queryParams.cardStatus = null
    },
    // 删除业务
    async delBtn(id) {
      // 1. 发送请求
      await delCardAPI(id)
      // console.log('删除成功')
      // 2. 提示用户
      this.$message.success('删除成功')
      // 3. 边界处理
      if (this.list.length === 1 && this.queryParams.page > 1) {
        this.queryParams.page--
      }
      // 4. 重新获取数据 -- 更新视图
      this.getCardList()
    },
    // 选择条目变化
    async handleSelectionChange(value) {
      // console.log(value)
      this.selectedCarList = value.map(item => item.id)
      // console.log(this.selectedCarList)
    },
    // 批量删除
    async delBtns() {
      // 1. 发送请求
      await delCardAPI(this.selectedCarList.join())
      console.log('批量删除成功')
      // 2. 提示用户
      this.$message.success('批量删除成功')
      // 3. 边界处理
      if (this.list.length === this.selectedCarList.length && this.queryParams.page > 1) {
        this.queryParams.page--
      }
      // 4. 重新获取数据 -- 更新视图
      this.getCardList()
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
